import {Tip} from "../Thing/Tip.js"
class EditHelper {
	constructor(viewer) {
		this._viewer = viewer;
		this._Tip=null;
		
		this._position=null;
		this._positions=[];
		this._graphics=null;
		this._drawType=null;
		this._options=null;
	}
	
	/**
	 * @description 设置绘制参数
	 * @param {Object} options{
		 type: polyline-多线段 polygon-多边形 point-点,
		 style:{
		 }
		 clampToGround:true/false,
	 }
	 */
	start(options) {
		let _this=this;
		_this._options=options;
		if(!Cesium.defined(options)){
			console.log('参数必填!');
			return;
		}
		
		if(!Cesium.defined(options.type)){
			console.log('请指定绘制类型!');
			return;
		}
		_this._drawType=options.type;
		_this._viewer.event.pauseAll();
		this._Tip=new Tip(this._viewer);
		console.log('避免事件冲突，绘制时暂时禁用其它事件。');
		this._handler = new Cesium.ScreenSpaceEventHandler(this._viewer.scene.canvas);
		this._handler.setInputAction(function (movement) {
			let cartesian3=_this._viewer.util.convertWindowToWorld(movement.position, _this._graphics) 
			if(Cesium.defined(cartesian3)){
				if('point' != _this._drawType){
					if (_this._positions.length == 0) {
						_this._positions.push(cartesian3.clone());
					}
				}
				_this._positions.push(cartesian3);
			}
		},Cesium.ScreenSpaceEventType.LEFT_CLICK)
		
		
		this._handler.setInputAction(function (movement) {
			_this._positions.pop();
			if (_this._handler) {
				_this._handler.destroy();
				_this._handler=null;
			}
			if(_this._Tip){
				_this._Tip.destroy();
				_this._Tip=null;
			}
		},Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK)
		
		this._handler.setInputAction(function (movement) {
			if('point' == _this._drawType){
				_this._Tip.showAt(movement.endPosition,"单击添加点");
				let cartesian3=_this._viewer.util.convertWindowToWorld(movement.endPosition, _this._graphics)
				if(Cesium.defined(cartesian3)){
					_this._position=cartesian3;
					if(!_this._graphics){
						_this.create(_this);
					}
				}
			}
			else{
				if (_this._positions.length == 0){
					_this._Tip.showAt(movement.endPosition,"单击开始绘制");
					return;
				}
				_this._Tip.showAt(movement.endPosition,"单击添加点，双击完成绘制");
				let cartesian3=_this._viewer.util.convertWindowToWorld(movement.endPosition, _this._graphics) 
				if(Cesium.defined(cartesian3)){
					if(!_this._graphics){
						_this.create(_this);
					}
					_this._positions.pop();
					_this._positions.push(cartesian3);
				}
				
			}
		}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
		
		this._handler.setInputAction(function (movement) {
			
			
		}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
		
		//_this._viewer.event.resumeAll();
	}
	draw(_this,cartesian3){
		if(_this._positions.length >=2){
			if(!_this._graphics){
				if(0==_this._drawType){
					
				}
				else if(1==_this._drawType){
					
				}
			}
		}
		_this._positions.pop();
		_this._positions.push(cartesian3);
	}
	create(){
		let _this=this;
		if('point'==_this._drawType){
			_this._options.graphics.position=new Cesium.CallbackProperty(function () {
					return _this._position;
			},false)
		}
		else if('polyline'==_this._drawType){
			_this._options.graphics.polyline.positions=new Cesium.CallbackProperty(function () {
					return _this._positions;
			},false)
		}
		else if('polygon'==_this._drawType){
			_this._options.graphics.polygon.hierarchy=new Cesium.CallbackProperty(function () {
					return new Cesium.PolygonHierarchy(_this._positions);
			},false)
		}
		_this._graphics =_this._viewer.graphicsLayer.add({
			graphics:_this._options.graphics,
			group:_this._options.group
		});
		
	}
	edit(){
		// if('point'==_this._drawType){
		// 	this._viewer.graphicsLayer.add({
		// 		graphics:this._options.graphics,
		// 		group:this._options.group
		// 	});
		// }
		// else if('polyline-'==_this._drawType){
		// 	_this._graphics = _this._viewer.entities.add({
		// 	  polyline: {
		// 	    positions:new Cesium.CallbackProperty(function () {
		// 			return _this._positions;
		// 		},false),
		// 	    width: 2,
		// 	    material: Cesium.Color.RED,
		// 	    clampToGround: true,
		// 	  },
		// 	});
		// }
		// else if('polygon'==_this._drawType){
		// 	_this._graphics = _this._viewer.entities.add({
		// 		polygon: {
		// 			hierarchy: new Cesium.CallbackProperty(function () {
		// 				return new Cesium.PolygonHierarchy(_this._positions);
		// 			},false),
		// 			extrudedHeight: 0,
		// 			perPositionHeight: true,
		// 			outline:true,
		// 			outlineColor:Cesium.Color.YELLOW,
		// 			outlineWidth:2,
		// 			material:new Cesium.ColorMaterialProperty(Cesium.Color.RED.withAlpha(0.5))
		// 		}
		// 	});
		// }
	}
	/**
	 * @description 取消绘制模式
	 * @param {Object} fn
	 */
	cancel(fn) {}

	/**
	 * @description 绘制结束
	 * @param {Object} fn
	 */
	finish(fn) {
	}
	error(fn){
		this.error=fn;
	}
	/**
	 * @description 销毁
	 */
	destroy() {
		this.cancel();
		delete this._viewer
		return Cesium.destroyObject(this);
	}
}
export {
	EditHelper
}
